<!--
 * @Descripttion: 编辑推广图
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-08-20 15:22:31
-->
<template>
  <div class="lb-edit-card">
    <top-nav :title="navTitle" :isBack="true"></top-nav>
    <div class="page-main">
      <div style="height: 20px"></div>
      <el-form
        @submit.native.prevent
        :model="subForm"
        :rules="subFormRules"
        ref="aForm"
        label-width="140px"
        class="basic-form"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="subForm.title"
            maxlength="20"
            show-word-limit
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="活动图片" prop="type">
          <el-radio-group v-model="subForm.type">
            <el-radio :label="0">封面图</el-radio>
            <el-radio :label="1">详情图</el-radio>
            <el-radio :label="2">分享图</el-radio>
          </el-radio-group>
          <lb-tool-tips>{{
            subForm.type === 0
              ? '展示在名片页面的新客福包弹窗封面图'
              : subForm.type === 1
              ? '展示在新客福包详情页面的图片'
              : '用于分享微信或保存海报的图片'
          }}</lb-tool-tips>
        </el-form-item>
        <el-form-item label="" prop="img">
          <lb-cover
            :fileList="subForm.img"
            @selectedFiles="getCover"
          ></lb-cover>
          <lb-tool-tips>{{
            subForm.type === 0
              ? '图片建议尺寸: 630 * 800'
              : subForm.type === 1
              ? '图片建议尺寸: 750 * n'
              : '图片建议尺寸: 622 * 690'
          }}</lb-tool-tips>
        </el-form-item>
        <el-form-item>
          <lb-button
            @click="submitFormInfo"
            type="primary"
            v-if="subForm.status_text !== 3"
            >{{ $t('action.save') }}</lb-button
          >
          <lb-button @click="goBack">{{ $t('action.back') }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      id: 0,
      navTitle: '',
      // 基本信息
      subForm: {
        title: '',
        type: 0,
        img: []
      },
      subFormRules: {
        title: { required: true, type: 'string', message: '请输入标题', trigger: 'blur' },
        type: { required: true, type: 'number', message: '请选择', trigger: 'blur' },
        img: { required: true, type: 'array', message: '请上传图片', trigger: 'change' }
      }
    }
  },
  async created () {
    let { id } = this.$route.query
    if (id) {
      this.id = id
      await this.getDetail(id)
    }
    this.navTitle = this.$t(id ? 'menu.CashEditImage' : 'menu.CashAddImage')
  },
  methods: {
    getCover (imgs) {
      this.subForm.img = imgs
    },
    goBack () {
      this.$router.back(-1)
    },
    async getDetail (id) {
      let { code, data } = await this.$api.cashImgUpdate({ id })
      if (code !== 200) return
      data.img = [{ url: data.img }]
      this.subForm = data
    },
    async submitFormInfo () {
      let subForm = JSON.parse(JSON.stringify(this.subForm))
      this.$refs['aForm'].validate(valid => {
        if (!valid) return
        subForm.img = subForm.img[0].url
        let method = subForm.id ? 'cashImgUpdate' : 'cashImgAdd'
        this.$api[method](subForm).then(res => {
          if (res.code !== 200) return
          this.$message.success(this.$t('tips.successSub'))
          this.goBack()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-edit-card {
  width: 100%;
  .el-input,
  .el-select,
  .lb-input-number,
  .el-cascader {
    width: 400px;
  }
}
</style>
